package com.beagle.njddapp.ui.presenter;

import com.amap.api.services.core.PoiItem;
import com.beagle.njddapp.base.ApiService;
import com.beagle.njddapp.base.BasePresenter;
import com.beagle.njddapp.base.DirverApiService;
import com.beagle.njddapp.base.NjOwnerApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.dirver.OilOrderState;
import com.beagle.njddapp.bean.owner.DIrverOrderBean;
import com.beagle.njddapp.core.LocationManager;
import com.beagle.njddapp.net.BaseOilHttpListResponse;
import com.beagle.njddapp.net.BaseOilHttpResponse;
import com.beagle.njddapp.net.BaseStringResponseBean;
import com.beagle.njddapp.net.HttpResponseObserver;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.ui.view.DirverDetailView;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.LinkedHashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;


public class DirverDetailPresenter extends BasePresenter<DirverDetailView> {
    private NjOwnerApiService njOwnerApiService;
    private DirverApiService dirverApiService;
    private ApiService apiService;

    public DirverDetailPresenter() {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        //获取接口对象
        njOwnerApiService = retrofit.create(NjOwnerApiService.class);
        dirverApiService = retrofit.create(DirverApiService.class);
        apiService = retrofit.create(ApiService.class);

    }

    public void getList() {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("orderId", "");
        paramsMap.put("orderNum", "");
        paramsMap.put("orderStatus", "");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        paramsMap.clear();
        njOwnerApiService.queryFindDriverOrderList(1000+"",1+"",body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpListResponse<DIrverOrderBean>>() {
            @Override
            public void success(BaseOilHttpListResponse<DIrverOrderBean> orderBeanBaseOilHttpListResponse) {
                if (orderBeanBaseOilHttpListResponse!=null){
                    getView().showOrderList(orderBeanBaseOilHttpListResponse.results);
                }else {
                    getView().onError("");

                }
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }

    public void updateFindDriverOrder(DIrverOrderBean dIrverOrderBean){
        Gson gson=new Gson();
        String strEntity = gson.toJson(dIrverOrderBean);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        njOwnerApiService.updateFindDriverOrder(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse>() {
            @Override
            public void success(BaseOilHttpResponse baseOilHttpResponse) {
                getView().updateSuccess();
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }
    public void meshState(DIrverOrderBean dirverHomeBean){
        Gson gson=new Gson();
        String strEntity = gson.toJson(dirverHomeBean);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        dirverApiService.meshState(body).compose(RxSchedulers.compose()).safeSubscribe(new Observer<OilOrderState>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(OilOrderState o) {
                getView().meshStatus(o);
            }

            @Override
            public void onError(Throwable e) {
                getView().onError("");
            }

            @Override
            public void onComplete() {

            }
        });
    }

    public void deviceGiveBack(int id){
        Gson gson=new Gson();
        HashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("orderId", id+"");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        dirverApiService.deviceGiveBack(body).compose(RxSchedulers.compose()).safeSubscribe(new Observer<BaseOilHttpResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseOilHttpResponse o) {
                getView().giveBackSuccess();
            }

            @Override
            public void onError(Throwable e) {
                getView().onError("");
            }

            @Override
            public void onComplete() {

            }
        });
    }


    public void updateFindDriverOrderDate(String reason,int id,String  date){
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("reason", reason);
        paramsMap.put("orderId", id+"");
        paramsMap.put("endDate", date);
        String strEntity = gson.toJson(paramsMap);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        dirverApiService.updateFindDriverOrderDate(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse>() {



            @Override
            public void success(BaseOilHttpResponse baseOilHttpResponse) {
                getView().showUpdateSuccess();
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }

        });
    }
    public void updateFindDriverOrderDate(int id){
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("orderId", id+"");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        dirverApiService.updateFindDriverOrderDate(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse>() {



            @Override
            public void success(BaseOilHttpResponse baseOilHttpResponse) {
                getView().showUpdateSuccess();
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }

        });
    }

    public void createRequest(String meshId, String count, PoiItem poiItem,String address){

        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new LinkedHashMap<>();
        paramsMap.put("type","fuelRequest");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        apiService.requestFormToken(body).compose(RxSchedulers.compose()).safeSubscribe(new Observer<BaseStringResponseBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseStringResponseBean baseStringResponseBean) {
                if (baseStringResponseBean.getResult()!=null){

                    Gson gson=new Gson();
                    HashMap<String,String> paramsMap=new HashMap<>();
                    paramsMap.put("meshId",meshId);
                    paramsMap.put("latitude",poiItem==null? LocationManager.getInstance().getLatitude()+"":poiItem.getLatLonPoint().getLatitude()+"");
                    paramsMap.put("longitude",poiItem==null?LocationManager.getInstance().getLongitude()+"":poiItem.getLatLonPoint().getLongitude()+"");
                    paramsMap.put("address", address);
                    paramsMap.put("expectMeter",count);
                    paramsMap.put("formToken", baseStringResponseBean.getResult());
                    String strEntity = gson.toJson(paramsMap);
                    RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
                    dirverApiService.createRequest(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse>() {
                        @Override
                        public void success(BaseOilHttpResponse o) {
                            getView().SqSuccess();
                        }

                        @Override
                        public void error(int code, String message) {
                            getView().onError(message);
                        }
                    });
                }
            }

            @Override
            public void onError(Throwable e) {
                getView().onError("");

            }

            @Override
            public void onComplete() {

            }


        });


    }


}